<template>
  <div class="ubBody usuallyBody wap">
    <div>
      <x-header :left-options="{backText: ''}">
        <span>常购清单</span>
      </x-header>
    </div>
    <div class="goodsListBox main">
      <scroller v-if="dataArr.length>0" class="scrollerStyle" :on-refresh="refresh" :on-infinite="infinite"
                ref="my_scroller_tab" :noDataText="this.$noData(dataArr)">

        <good-item-two :list="dataArr" :shopZeroSaleState="shopZeroSaleState"></good-item-two>

      </scroller>
      <div v-else class="display-flex flex-direction-row align-items-center justify-content-flex-center">
        <!--<img @click="initData(0)" style="width: 3rem;margin-top: 2rem" src="../../../assets/img/customer/data_empty.png" alt="">-->
        <span style="margin-top: 2rem;font-size: 16px;color:#666666">暂无常购商品</span>
      </div>
    </div>
  </div>
</template>

<script>
  import goodItemTwo from "../../../components/good-item-two.vue"

  export default {
    name: "usuallyBuyList",
    components: {
      goodItemTwo
    },
    data() {
      return {
        shopZeroSaleState: '',
        pageNum: 1,
        noData: false,
        dataArr: [],
        isScroll:true
      }
    },
    created() {
      //获取经销商信息
      this.$ajax.get('common/distributor/info', res => {
        if (res.code == '200') {
          // console.log('经销商信息：',res)
          this.shopZeroSaleState = res.data.zeroSaleFlag
        }
      }, {distributorId: this.$route.query.id});

      this.initData(0);
    },
    mounted() {
      if(this.$refs.my_scroller_tab){
        this.$refs.my_scroller_tab.finishPullToRefresh();
      }
      var that=this;
      document.getElementsByClassName('usuallyBody')[0].addEventListener('touchmove', this.touchMoveScroll,false)
    },
    destroyed(){
      if(document.getElementsByClassName('usuallyBody')[0]){
        document.getElementsByClassName('usuallyBody')[0].removeEventListener('touchmove',this.touchMoveScroll,false)
      }
    },
    beforeRouteLeave(to, from, next) {//记录离开时的位置
      let currentPosition = this.$refs.my_scroller_tab && this.$refs.my_scroller_tab.getPosition() && this.$refs.my_scroller_tab.getPosition().top;
      sessionStorage.usuallyPositon=currentPosition?currentPosition:0
      console.log("usuallyPositon", sessionStorage.usuallyPositon)
      next()
    },
    methods: {
      touchMoveScroll(){
        this.isScroll=false;
        setTimeout(()=>{
          this.isScroll=true;
        },3000)
      },
      deWeight(arr) {
        //对象数组去重
        if (arr == '' || arr == null) {
          arr = [];
        }
        const res = new Map();
        return arr.filter((a) => !res.has(a.type) && res.set(a.type, 1));
      },
      getScroll(){
        setTimeout(() => {
          if (this.$refs.my_scroller_tab&&sessionStorage.usuallyPositon) {
            this.$refs.my_scroller_tab.scrollTo(0, sessionStorage.usuallyPositon, true);
          }
        },50)
      },

      initData(scrollType) {
        let params = {
          sellerId: this.$route.query.id,
          pageNo: this.pageNum,
          pageSize: 10
        };
        this.$ajax.get('replenish/bug_history/list', res => {
          if (res.code == '200') {
            // this.dataArr = res.data;
            for (let i = 0; i < res.data.length; i++) {
              for (let j = 0; j < res.data[i].unitVOList.length; j++) {
                if (!res.data[i].unitVOList[j].shoppingCartVO) {
                  res.data[i].unitVOList[j].shoppingCartVO = {num: 0}
                }
                if (this.shopZeroSaleState == 0 && res.data[i].zeroSaleFlag == 0) {
                  res.data[i].unitVOList[j].stockFlag = false;
                } else {
                  if (res.data[i].unitVOList[j].shoppingCartVO.num >= res.data[i].unitVOList[j].goodsStock) {
                    res.data[i].unitVOList[j].stockFlag = true;
                  } else {
                    res.data[i].unitVOList[j].stockFlag = false;
                  }
                }
              }
            }
            if (0 == scrollType) {
              this.dataArr = res.data;
            } else {
              if (res.data && res.data != '') {
                this.dataArr = this.dataArr.concat(res.data);
              } else {
                this.noData = true;
              }
            }

            if(this.isScroll){
              this.getScroll();
            }
          } else {
            this.$vux.toast.text(res.msg, 'center');
          }
        }, params)
      },
      goDetail(item) {
        // console.log(item)
        //this.$vux.toast.text(this.$fromApp(this), 'center');
        if (this.$fromApp(this)) {
          // this.$callJava({url:this.$webUrl+"good/operate?from=app&id="+item.id})
          this.$router.push({path: "/good/operate", query: {goodId: item.id, from: 'app'}})
        } else {
          this.$router.push({path: "/good/operate", query: {goodId: item.id}})
        }
      },
      reduceFn(val, item) {
        if (this.shopZeroSaleState == 0 && item.zeroSaleFlag == 0) {
          val.stockFlag = false;
        } else {
          if (val.shoppingCartVO.num >= val.goodsStock + 1) {
            val.stockFlag = true;
          } else {
            val.stockFlag = false;
          }
        }
        let count;
        if (val.shoppingCartVO.num != 0) {
          count = Number(val.shoppingCartVO.num) - 1;
          val.shoppingCartVO.num = count;
        }
        this.$ajax.post('replenish/shopping_cart/setting', {
          goodsId: val.unitGoodsPricesVO.goodsId,
          unitId: val.unitGoodsPricesVO.unitId,
          num: count
        }, res => {
          // console.log(res);
          if (res.code == 200) {
            this.$root.eventHub.$emit('updateCart')
          } else {
            this.$vux.toast.text(res.msg, 'center');
          }
        })
      },
      addFn(val, item) {
        console.log(val)
        if (this.shopZeroSaleState == 0 && item.zeroSaleFlag == 0) {
          val.stockFlag = false;
        } else {
          if (val.shoppingCartVO.num >= val.goodsStock - 1) {
            val.stockFlag = true;
          } else {
            val.stockFlag = false;
          }
        }
        let count;
        if (val.shoppingCartVO.num != 0) {
          count = Number(val.shoppingCartVO.num) + 1;
          val.shoppingCartVO.num = count;
        } else {
          count = 1;
          val.shoppingCartVO.num = 1;
        }
        this.$ajax.post('replenish/shopping_cart/setting', {
          goodsId: val.unitGoodsPricesVO.goodsId,
          unitId: val.unitGoodsPricesVO.unitId,
          num: count
        }, res => {
          // console.log(res);
          if (res.code == 200) {
            this.$root.eventHub.$emit('updateCart')
          } else {
            this.$vux.toast.text(res.msg, 'center');
          }
        })
      },
      inputCountFn(val, item) {

        this.$vux.confirm.show({
          title: '请输入商品数量',
          value: true,
          "showInput": true,
          "inputAttrs": {type: "number"},
          onCancel: () => {

          },
          onConfirm: (e) => {
            if (this.$checkIsNumber(e)) {
              if (e.length > 8) {
                this.$vux.toast.text("最多输入8位", 'center');
                return
              }
              this.$ajax.post('replenish/shopping_cart/setting', {
                goodsId: val.unitGoodsPricesVO.goodsId,
                unitId: val.unitGoodsPricesVO.unitId,
                num: e
              }, res => {
                if (res.code == 200) {
                  this.$root.eventHub.$emit('updateCart')
                  val.shoppingCartVO.num = e;

                  if (this.shopZeroSaleState == 0 && item.zeroSaleFlag == 0) {
                    val.stockFlag = false;
                  } else {
                    if (val.shoppingCartVO.num >= val.goodsStock) {
                      val.stockFlag = true;
                    } else {
                      val.stockFlag = false;
                    }
                  }
                } else {
                  this.$vux.toast.text(res.msg, 'center');
                }
              })
            } else {
              this.$vux.toast.text("请输入正整数", 'center');
            }
          }
        })
      },
      refresh() {
        this.pageNum = 1;
        this.noData = false;
        sessionStorage.usuallyPositon=0
        this.initData(0);
        if(this.$refs.my_scroller_tab){
          this.$refs.my_scroller_tab.finishPullToRefresh();
        }
      },
      infinite(done) {
        setTimeout(() => {
          if (!this.noData) {
            this.pageNum++;
            this.initData(1);
            done(true)
          } else {
            this.$refs.my_scroller_tab && this.$refs.my_scroller_tab.finishInfinite()
            done(true)
          }
        }, 600)
      }
    }
  }
</script>

<style type="text/less" lang="less" scoped>
  .ubBody {
    background-color: #ffffff;
  }
</style>
